<?php

namespace app\api\controller\v2;

use app\api\controller\mall\CommonMall;
use think\Db;
use think\Request;
use think\Validate;

class Mall extends CommonMall {
    /**
     * @var mixed
     */
    public $params = null;
    /**
     * @param Request $request
     */
    public function __construct(Request $request = null) {
        parent::__construct($request);
        $this->params = getRsaPostParamArr2();
    }

    public function receive()
    {
        $order_id = $this->params['order_id'] ?? '';
        // {"receiver":"哈哈","mobile":"18800188002","area":"重庆市重庆市万州区","address":"给哈狗和高后给哈狗和高后"}
        $post['receiver'] = $this->params['receiver'] ?? '';
        $post['mobile'] = $this->params['mobile'] ?? '';
        $post['area'] = $this->params['area'] ?? '';
        $post['address'] = $this->params['address'] ?? '';


        $validate = new Validate([
            ['receiver', 'require|chsAlpha|length:1,50', '收货人不能为空|收货人名称只能字母和汉字|收货人名称在1-50字符之间'],
            ['mobile', 'require', '手机不能为空'],
            ['area', 'require', '地区不能为空'],
            ['address', 'require', '详细地址不能为空'],
        ]);

        if (!$validate->check($post)) {
            return $this->miss($validate->getError());
        }

        $order = $this->db->name('mall_order')->where(['id'=>$order_id,'status'=>1])->find();
        if (!$order) return $this->miss('订单不存在');
        if ($order['address']) return $this->miss('已填写过地址');
        if ($order['to_user'] != $this->member_relang['id']) return $this->miss('等待接收方填写地址');

        $update['uid'] = $order['to_user'];
        $update['to_user'] = $order['uid'];
        $update['address'] = json_encode($post, JSON_UNESCAPED_UNICODE);

        $res = $this->db->name('mall_order')->where(['id'=>$order_id])->update($update);
        if ($res) return $this->pass('填写地址成功,等待发货');
        return $this->miss('填写地址失败,请重试');
    }

    public function friendPayChat()
    {
        $order_id = $this->params['order_id'] ?? '';
        $to_user = $this->params['to_user'] ?? '';
        //$data = $this->db->name('mall_order')->master()->where(['id' => $order_id, 'status' => 0])->lock(true)->find();
        $data = $this->db->name('mall_order')->master()->where(['id' => $order_id, 'status' => 0])->find();
        if (!$data) return $this->miss('订单取消或已支付');
        $mallItem = $this->db->name('mall_order_item')->where(['order_id'=>$data['id']])->select();
        $goods = [];
        $moreGoods = sizeof($mallItem);
        $goodsnum = 0;
        $goodsTotal = 0;
        foreach ($mallItem as $mkey => $mvalue) {
            if (empty($goods)) {
                $goods = $this->db->name('mall_goods')->find($mvalue['goods_id']);
            }
            $goodsnum += $mvalue['num'];
        }

        $datas = [
            'userid' => $this->member_relang['id'],
            'touserid' => $to_user,
            'type' => 19,
            'content' => '[商品代付]',
            'data' => json_encode(['order_id' => $order_id, 'name' => $goods['name'] . ($moreGoods > 1 ?" 等多件商品":""), 'image' => $goods['image'], 'num' => $goodsnum, 'money' => $data['total']], JSON_UNESCAPED_UNICODE)
        ];
        
        $request = request();
        $url = $request->domain() . str_replace('/index.php', '', $request->baseFile()) . '/apis/home/privatechat/addtalk';
        curl($url, json_encode($datas, JSON_UNESCAPED_UNICODE));

        return $this->pass('发送成功');
    }
}